﻿//建立一個可存取到該file的url
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}

/**
 * 图像上传
 * @param {object} aElement
 * @param {object} fileElement
 */
function ImageUpload(aElement, fileElement,imageViewer) {
    this.aElement = aElement;
    this.fileElement = fileElement;
    this.fileElement.change(function () {
        var objUrl = getObjectURL(this.files[0]);
        if (objUrl) {
            imageViewer.attr("src", objUrl);
        }
    });
    this.aElement.get(0).addEventListener("click", imageUploadClick, false);
    function imageUploadClick(e) {
        var file = $(this).attr("data-file");
        var fileElem = $("#" + file).get(0);
        if (fileElem) {
            fileElem.click();
        }
        e.preventDefault();
    }
}
